<?php
include_once(dirname(__FILE__) . "/../../configure/configure.php");		//--> global var
include_once(dirname(__FILE__) . "/../configure/admin.config.inc.php");	//--> admin global var
include_once(dirname(__FILE__) . "/../tools/smarty/libs/Smarty.class.php");					//--> out template

include_once("impl/CategoryImpl.php");

require_once(dirname(__FILE__) . "/../controlHeader.php");					//--> system control header
require_once(dirname(__FILE__) . "/../controlOperate.php");				//-->

$oCatImpl 	= new CategoryImpl(DB_TAG_MM);		//--> Info Class


$error_message 	= '';
$name 			= '';
$parentId		= '';
$isLeaf			= '1';
$status			= 1;


if(isset($_POST['Submit'])){
	$parentId 	= $_POST['parentid'];
	$name 		= $_POST['name'];
	$isLeaf 	= $_POST['isLeaf'];
	$title		= $_POST['title'];
	$descript 	= $_POST['descript'];
	$status 	= $_POST['status'];
	
	if($parentId == "")
		$error_message = 'The UP to level class should\'t be empty.';
	else if(empty($name))
		$error_message = 'The class name should\'t be empty.';
	else{
		$oCat = new CategoryDto();
		$oCat->parentId	= $parentId;
		$oCat->name 	= $name;
		$oCat->title	= $title;
		$oCat->isLeaf 	= $isLeaf;
		$oCat->descript	= $descript;
		$oCat->status	= $status;	// default online 
		
		$isReturn = $oCatImpl->add($oCat);
		if($isReturn)
			$error_message = 'add successfully.';
		else 
			$error_message = 'add failure.';
	}
}

$arrParentid 	= array('0' => "root");
$arrIsLeaf 		= array('1' => 'Yes', '0' => 'No');
$arrStatus 		= array('1' => 'Online', '0' => 'Offline');

$arrTmpStack = array();
$arrLevelStack = array();
$i = 0;
array_push($arrTmpStack,'0');

while(sizeof($arrTmpStack) > 0){
	$strtmp = array_pop($arrTmpStack);
	$i = array_pop($arrLevelStack);
	if($strtmp > 0){
		$oCat = $oCatImpl->getById($strtmp);
		if(is_object($oCat)){
			$tmp = '';
			for($j = 0; $j < $i; $j++){
				$tmp.='&nbsp;&nbsp;&nbsp;&nbsp;';
			}
			$arrParentid[$oCat->id] = $tmp . $oCat->name;
		}
	}

	$sql = "SELECT id FROM category WHERE parentid='$strtmp' ORDER BY name";
	$mixBack = $oCatImpl->getColumns(DB_TAG_MM, $sql);
	if(is_array($mixBack)){
		$i++;
		foreach($mixBack as $catId){
			array_push($arrTmpStack, $catId);
			array_push($arrLevelStack, $i);
		}
	}
}

/*----- out html -----*/
$smarty = new Smarty();		//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = COMPILE_SYS_DIR;
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);

$smarty->assign('backurl',$backurl);
$smarty->assign('error_message', $error_message);

$smarty->assign('arrIsLeaf', $arrIsLeaf);
$smarty->assign('isLeaf', $isLeaf);

$smarty->assign('arrStatus', $arrStatus);
$smarty->assign('status', $status);

$smarty->assign('arrParentid', $arrParentid);
$smarty->assign('parentId', $parentId);

$smarty->assign('title', $title);
$smarty->assign('name', $strName);
$smarty->assign('descript', $descript);

$smarty->display('listClass_Add.htm');
?>
